const format = (date, fmt) => {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
    }
  }
  return fmt
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length)
}
//格式化日期
export const formatDate = time => {
  if (time == '' || time == null || time == undefined) return '';
  if (typeof time === 'string') time = Number(time * 1000)
  let date = new Date(time);
  return format(date, 'yyyy-MM-dd hh:mm:ss')
}

//格式化金额
export const price = value => {
  if (!value) return '0.00';
  const _value = parseFloat(value);
  if (isNaN(_value)) return value;
  return _value.toFixed(2);
}

//格式化时间日期取值(日期 无时分秒)
export const formatTime = time => {
  if (time == '' || time == null || time == undefined) return '';
  if (typeof time === 'string') time = parseInt(time)
  let date = new Date(time);
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  month = month < 10 ? "0" + month : month;
  day = day < 10 ? "0" + day : day;
  return year + '-' + month + '-' + day;
}
//随机逻辑
export const randomNum = (min, max)=> {
  return Math.floor(Math.random() * (max - min + 1)) + min
}
// 转换日期
export const getBeforeTime = time => {
  if (time === undefined || time === null) {
    return;
  }
  /* 
    IOS只识别 2019-03-01 这样的日期格式, 不支持 2019-03-01 00:00:00
    用正则替换2019-03-01日期格式为2019/03/01后问题解决
  */
  time = (time).replace(/-/g, '/');
  var result = ''
  var minute = 1000 * 60 //分钟
  var hour = minute * 60 //小时 
  var day = hour * 24 //天数
  var month = day * 30 // 月数
  var now = new Date().getTime() //当前时间戳
  var dateTimeStamp = new Date(time).getTime() //某个时间戳
  var diffValue = now - dateTimeStamp
  if (diffValue < 0) return
  var monthC = diffValue / month
  var weekC = diffValue / (7 * day)
  var dayC = diffValue / day
  var hourC = diffValue / hour
  var minC = diffValue / minute
  // console.log('monthC', monthC, 'weekC', weekC, 'dayC', dayC, 'hourC', hourC, 'minC', minC)
  if (monthC >= 1) {
    result = "" + parseInt(monthC) + "月前"
  } else if (weekC >= 1) {
    result = "" + parseInt(weekC) + "周前"
  } else if (dayC >= 1) {
    result = "" + parseInt(dayC) + "天前"
  } else if (hourC >= 1) {
    result = "" + parseInt(hourC) + "小时前"
  } else if (minC >= 1) {
    result = "" + parseInt(minC) + "分钟前"
  } else {
    result = "刚刚"
  }
  return result
}

export const formatTime3 = (date,type) => {
  var date = new Date(date);
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  if(type==2){
    return year+"年"+month+"月"
  }else{
    return year+"-"+month
  }
  
}

export const Today = date => {
  var date = new Date(date);
  const day = date.getDate()
  return day
}

export const toHour = (seconds,type) => {
  if (!seconds) return 0;
  seconds=Number(seconds)
  if(seconds<3600){
    var mins=(seconds/60).toFixed(2)?(seconds/60).toFixed(2):1
    type=type?type:'min'
    return mins + type
  }else{
    var hour=(seconds/60/60).toFixed(2)
    type=type?type:'h'
    return hour + type
  }
}
